package com.abc.robot01.activity

import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.abc.robot01.R

/**
 * DemoActivity
 */
class DemoActivity : BaseVoiceActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MaterialTheme {
                    CustomNotificationOverlay {
                        Column {
                            Text("This is a custom notification!")
                            Text("Add any content here.")
                        }
                    }
                }
            }
        }

    override fun onPause() {
        super.onPause()
    }

    override fun onResume() {
        super.onResume()
    }

    @Composable
    fun CustomNotificationOverlay(
        content: @Composable () -> Unit
    ) {
        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(Color.Red)
        ) {
            Box(
                modifier = Modifier.clickable {
                    showToast(this@DemoActivity,"1","1","","")
                }
                    .align(Alignment.BottomStart)
                    .padding(16.dp)
                    .background(Color.White, shape = RoundedCornerShape(8.dp))
                    .border(1.dp, Color.Gray, RoundedCornerShape(8.dp))
                    .padding(16.dp)
            ) {
                content()
            }
        }
    }

    companion object {
        const val TAG = "DemoActivity"
    }


    private fun CustomComposeToast(message: String) {
        // Create a Toast and set a ComposeView as its custom view
        val toast = Toast(this)
        val composeView = LayoutInflater.from(this).inflate(R.layout.compose_toast, null) as ComposeView
        composeView.setContent {
            CustomToastContent(message)
        }
        toast.view = composeView
        toast.duration = Toast.LENGTH_SHORT
        toast.show()
    }

    @Composable
    fun CustomToastContent(message: String) {
        Box(
            modifier = Modifier
                .background(Color.Black, RoundedCornerShape(8.dp))
                .padding(16.dp)
        ) {
            Text(
                text = message,
                color = Color.White,
                fontSize = 16.sp,
                style = MaterialTheme.typography.bodyMedium
            )
        }
    }
}

fun showToast(context: Context,name: String,time:String,id:String,url:String){
    val toast = Toast(context)
    val inflater = LayoutInflater.from(context)
    val customLayout = inflater.inflate(R.layout.custom_toast_layout, null)
    customLayout.findViewById<TextView>(R.id.name).text = "渲染名称: $name";
    customLayout.findViewById<TextView>(R.id.time).text = "渲染时间: $time";

    customLayout.findViewById<Button>(R.id.view).setOnClickListener {
        context.startActivity<H5Activity>() {
        putExtra("id", id)
        putExtra("url", url)
    }
    }
    toast.view = customLayout
    toast.duration = Toast.LENGTH_LONG
    toast.setGravity(Gravity.BOTTOM or Gravity.RIGHT, 0, 0) // yOffset 可根据需要调整
    toast.show()
}

